package com.cx.controller.admin.basic;

import com.cx.service.RedisService;
import com.cx.service.admin.basic.ChainBasicOrgManager;
import com.cx.utils.RedisKeyUtil;
import org.cx.base.ApiModel;
import org.cx.base.BaseController;
import org.cx.constant.BasicConstants;
import org.cx.constant.ServerCodeType;
import org.cx.entity.Page;
import org.cx.entity.PageData;
import org.cx.util.AssertUtil;
import org.cx.util.ExcelUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

@RestController("admin/basic/org")
@RequestMapping("admin/basic/org")
public class OrgController extends BaseController {


    @Resource
    private RedisService jedisService;

    @Resource
    private ChainBasicOrgManager chainBasicOrgService;

    @ResponseBody
    @RequestMapping(value = "/findOrgDetailPage")
    public ApiModel findOrgDetailPage() throws Exception {
        Page pg =this.getPage();
        PageData pd = (PageData) pg.getPd();
        if(pd.get("orgId")== null || pd.get("orgId").equals("")) {
            pd.put("orgId", getUserByToken(pd.getString("token")).get("orgId"));
        }
        pd.put("pid", getUserByToken(pd.getString("token")).get("orgId"));
        pg.setPd(pd);
        List<PageData> list = chainBasicOrgService.findOrgDetailPage(pg);
        pg.setPd(list);
        return buildSuccMsg(pg);
    }

    @ResponseBody
    @RequestMapping(value = "/findOrgInfoById")
    public ApiModel findOrgInfoById() throws Exception {
        PageData pd =this.getPageData();
        pd = chainBasicOrgService.findOrgByPageDate(pd);
     //   pg.setPd(list);
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/updateOrg")
    public ApiModel updateOrg() throws Exception {
        PageData pd =this.getPageData();
        Integer result = chainBasicOrgService.updateOrg(pd);
        //   pg.setPd(list);
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/insertOrg")
    public ApiModel insertOrg() throws Exception {
        PageData pd =this.getPageData();
        Integer result  = chainBasicOrgService.insertOrg(pd);
        //   pg.setPd(list);
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/findAllOrgsByUserorg")
    public ApiModel findAllOrgsByUserorg(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        PageData pd =this.getPageData();
        pd.put("pid",getUserByToken().get("orgId"));
        List<PageData> list = chainBasicOrgService.findAllOrgsByUserorg(pd);
        return buildSuccMsg(list);
    }

    @ResponseBody
    @RequestMapping(value = "/exportOrgDetail")
    public void exportOrgDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Page pg =this.getPage();
        PageData pd = (PageData) pg.getPd();
        AssertUtil.isNotNull(getUserByToken(request.getParameter("token")), ServerCodeType.USER_NOT_LOGIN.getMessage());
        if(pd.get("orgId")== null || pd.get("orgId").equals("")) {
            pd.put("orgId", getUserByToken(request.getParameter("token")).get("orgId"));
        }
        pd.put("pid", getUserByToken(request.getParameter("token")).get("orgId"));
        pg.setPd(pd);
        pg.setShowCount(BasicConstants.ExportData.maxNum);
        List<PageData> list = chainBasicOrgService.findOrgDetailPage(pg);
        String[] title ={"门店编码","门店名称","门店地址","店长姓名","店长手机","创建日期","门店电话"};
        String[] onlyFilds = {"id","name","addrCh","userName","userPhone","createTimeFormat","phone"};

        ExcelUtils.fillExcelForFile(list,title,onlyFilds,"orgDetail_",response);

    }



}
